package ywpt.web.action.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.CallRecord;
import ywpt.service.service.CallRecordService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class CallRecordAction extends BaseAction implements ModelDriven<CallRecord>, Preparable {

	/**
	 * 回访Action
	 */
	private static final long serialVersionUID = 4960361460706767590L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];//λȨ�����飬
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static final int DEFAULT_PAGE_SIZE = 100;
	private Long id;
	private CallRecord callRecord;
	private List<CallRecord> callRecordList = new ArrayList<CallRecord>();
	private CallRecordService  callRecordService;
 	
	private Long peopleId;
	private String peopleName;
	
	private Long usualId;
	private String usualName;
	
	private Long clientId;
	private String clientName;
	
	private Long checkId;
	private String checkName;
	
	private Long keshiId;
	private Long departId;
		
	//�������
	private String ids;
	
	//查询相关
	private String qRecordCode;
	private String qclient;
	private String qQuestions;
	private int qState;
	private String qStartDate;
	private String qEndDate;
	
	//选择框查询
	private String sqCode;
	private String sqClient;
	private String sqKeshi;
	private String sqDoor;
	private String sqLink;
	
	public String getSqCode() {
		return sqCode;
	}

	public void setSqCode(String sqCode) {
		this.sqCode = sqCode;
	}

	public String getSqClient() {
		return sqClient;
	}

	public void setSqClient(String sqClient) {
		this.sqClient = sqClient;
	}

	public String getSqKeshi() {
		return sqKeshi;
	}

	public void setSqKeshi(String sqKeshi) {
		this.sqKeshi = sqKeshi;
	}

	public String getSqDoor() {
		return sqDoor;
	}

	public void setSqDoor(String sqDoor) {
		this.sqDoor = sqDoor;
	}

	public String getSqLink() {
		return sqLink;
	}

	public void setSqLink(String sqLink) {
		this.sqLink = sqLink;
	}

	public int getQState() {
		return qState;
	}

	public void setQState(int state) {
		qState = state;
	}

	public String getQRecordCode() {
		return qRecordCode;
	}

	public void setQRecordCode(String recordCode) {
		qRecordCode = recordCode;
	}

	public String getQclient() {
		return qclient;
	}

	public void setQclient(String qclient) {
		this.qclient = qclient;
	}

	public String getQQuestions() {
		return qQuestions;
	}

	public void setQQuestions(String questions) {
		qQuestions = questions;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public String getPeopleName() {
		return peopleName;
	}

	public void setPeopleName(String peopleName) {
		this.peopleName = peopleName;
	}

	public Long getUsualId() {
		return usualId;
	}

	public void setUsualId(Long usualId) {
		this.usualId = usualId;
	}

	public String getUsualName() {
		return usualName;
	}

	public void setUsualName(String usualName) {
		this.usualName = usualName;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CallRecord getCallRecord() {
		return callRecord;
	}

	public void setCallRecord(CallRecord callRecord) {
		this.callRecord = callRecord;
	}

	public List<CallRecord> getCallRecordList() {
		return callRecordList;
	}

	public void setCallRecordList(List<CallRecord> callRecordList) {
		this.callRecordList = callRecordList;
	}

	public CallRecordService getCallRecordService() {
		return callRecordService;
	}

	public void setCallRecordService(CallRecordService callRecordService) {
		this.callRecordService = callRecordService;
	}
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public Long getCheckId() {
		return checkId;
	}

	public void setCheckId(Long checkId) {
		this.checkId = checkId;
	}

	public String getCheckName() {
		return checkName;
	}

	public void setCheckName(String checkName) {
		this.checkName = checkName;
	}

	public Long getKeshiId() {
		return keshiId;
	}

	public void setKeshiId(Long keshiId) {
		this.keshiId = keshiId;
	}
	
	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public String getQStartDate() {
		return qStartDate;
	}

	public void setQStartDate(String startDate) {
		qStartDate = startDate;
	}

	public String getQEndDate() {
		return qEndDate;
	}

	public void setQEndDate(String endDate) {
		qEndDate = endDate;
	}
	/*-----------------------------------------------------------------------------------------*/
	@Override
	public CallRecord getModel() {
		return this.callRecord;
	}

	@Override
	public void prepare() throws Exception {
		if(id!= null){
			this.callRecord = this.callRecordService.getCallRecordById(id);
		}else{
			this.callRecord = new CallRecord();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.SERVICE_PHONECALL);
	}
	protected void getdataList(DetachedCriteria query) {
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = callRecordService.getCallRecordCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("recordCode"));
		query.setProjection(null);
		callRecordList.addAll(callRecordService.getCallRecords(query, pageSize,
				rowStartEnd[0]));
	}
	protected void showInfo() {
		if(callRecord.getPeople() != null){
			this.setPeopleId(callRecord.getPeople().getId());
			this.setPeopleName(callRecord.getPeople().getPeopleName());
		}
		if(callRecord.getUsual() != null){
			this.setUsualId(callRecord.getUsual().getId());
			this.setUsualName(callRecord.getUsual().getTitle());
		}
		if (this.callRecord.getClient()!=null) {
			this.clientId = this.callRecord.getClient().getId();
			this.clientName = this.callRecord.getClient().getClientName();
		}
		if (this.callRecord.getChecker()!=null) {
			this.checkId = this.callRecord.getChecker().getId();
			this.checkName = this.callRecord.getChecker().getPeopleName();
		}
		if (this.callRecord.getDepart()!=null) {
			this.departId = this.callRecord.getDepart().getId();
		}
	}
	//**********************************************************	
	public String list() throws Exception{
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class);
		getdataList(query);
		return SUCCESS;
	}	
	public String input() 
	{
		String recordCode =this.callRecordService.createRecordCode();
		this.callRecord.setRecordCode(recordCode);
		if(this.getCurrentUser().getPeople() != null){
			this.setPeopleId(this.getCurrentUser().getPeople().getId());
			this.setPeopleName(this.getCurrentUser().getPeople().getPeopleName());
		}
		this.departId = (long)3;
		//this.departId = this.getCurrentUser().getPeople().getDepart().getId();
		this.callRecord.setRecordDate(new Date());
		this.callRecord.setCerateDate(new Date());
		return INPUT;
	}	

	/**
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{	
		try 
		{
			if (id == null) 
			{
				Boolean exist = callRecordService.checkCode(callRecord.getRecordCode());
				if (!exist) {
					callRecordService.saveCallRecord(callRecord,peopleId,clientId,usualId,checkId,keshiId,departId);
					this.addActionMessage(SystemMessage.SAVE_SUCCESS);					
				}else {
					this.addActionMessage("编号重复！请重新录入！");
				}

				
			} 
			else 
			{   
				callRecordService.updateCallRecord(callRecord,peopleId,clientId,usualId,checkId,keshiId,departId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			showInfo();
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	
	public String edit() 
	{
		callRecord = this.callRecordService.getCallRecordById(id);
		showInfo();
		return INPUT;
	}	
	
	/**
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			this.callRecordService.removeCallRecord(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	
	public String selectList() {
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class);
		query.add(Restrictions.eq("state", 1));		
		if (!StringTools.isNullOrSpace(this.sqCode)) {
			query.add(Restrictions.like("recordCode", "%"+this.sqCode+"%"));
		}
		if (!StringTools.isNullOrSpace(this.sqClient)) {
			query.createAlias("client", "client").add(Restrictions.like("client.clientName", "%"+this.sqClient+"%"));		
		}
		if (!StringTools.isNullOrSpace(this.sqKeshi)) {
			query.add(Restrictions.like("keshiName", "%"+this.sqKeshi+"%"));	
		}
		if (!StringTools.isNullOrSpace(this.sqDoor)) {
			query.add(Restrictions.like("doorCode", "%"+this.sqDoor+"%"));
		}
		if (!StringTools.isNullOrSpace(this.sqLink)) {
			query.add(Restrictions.like("manPhone", "%"+this.sqLink+"%"));
		}
		getdataList(query);
		return SUCCESS;
	}
	
	public String search() throws Exception {
		callRecordList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class);
		if(!StringTools.isNullOrSpace(this.qRecordCode )){
			query.add(Restrictions.like("recordCode", "%"+this.qRecordCode+"%"));
		}	
		if(!StringTools.isNullOrSpace(this.qclient)){
			query.add(Restrictions.like("clientName", "%"+this.qclient+"%"));
		}	
		if(this.qState>0){
			query.add(Restrictions.eq("state", qState));
		}
		if(!StringTools.isNullOrSpace(this.qStartDate)&& !StringTools.isNullOrSpace(this.qEndDate))
		{				
			if(FORMAT.parse(this.qStartDate).before(FORMAT.parse(this.qEndDate)))
				query.add(Restrictions.between("cerateDate",FORMAT.parse(this.qStartDate),DateTools.addDay(FORMAT.parse(this.qEndDate), 0)));
			else  
				query.add(Restrictions.between("cerateDate",FORMAT.parse(this.qEndDate),DateTools.addDay(FORMAT.parse(this.qStartDate),0)));
		}
		if(!StringTools.isNullOrSpace(this.qQuestions )){
			query.add(Restrictions.like("questions", "%"+this.qQuestions+"%"));
		} 
		getdataList(query);
		return SUCCESS;
	}
	
	public String complete(){
		if(!StringTools.isNullOrSpace(this.ids)){
			String[] idStr = this.ids.split(";"); 
			for(String item: idStr){
				Long cId = Long.parseLong(item);
				callRecord = this.callRecordService.getCallRecordById(cId);
				callRecord.setState(2);
				Long peopleId = null,usualId = null;
				if(null != callRecord.getPeople()){
					  peopleId = callRecord.getPeople().getId();
				}
				if(null != callRecord.getUsual()){
					  usualId  = callRecord.getUsual().getId();
				}				
				this.callRecordService.updateCallRecord(callRecord, peopleId,clientId, usualId,checkId,null,null);
			}			
		}
		return SUCCESS;
	}

	public String select() {
		DetachedCriteria query = DetachedCriteria.forClass(CallRecord.class);
			query.add(Restrictions.eq("state", 1));
			this.callRecordList.addAll(this.callRecordService.getCallRecords(query));
		return SUCCESS;
	}

}
